//
//  SavePhotoManager.m
//  Unity-iPhone
//
//  Created by Brian on 2019/1/29.
//

#import "SavePhotoManager.h"
#import <UIKit/UIKit.h>
#import <Photos/Photos.h>

@implementation SavePhotoManager

void _SavePhoto(const char *path)
{
    NSString *filePath = [NSString stringWithUTF8String:path];
    if ([filePath hasSuffix:@".jpg"]) {
        UIImage *image = [UIImage imageWithContentsOfFile:[NSString stringWithUTF8String:path]];
        [SavePhotoManager saveToThumb:image];
    }
    else if ([filePath hasSuffix:@".mp4"]) {
        [SavePhotoManager saveVideoToThumb:filePath];
    }
}

//保存图片到相册
+ (void)saveToThumb:(UIImage *)image {
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

}

+ (void)saveVideoToThumb:(NSString *)path {
    UISaveVideoAtPathToSavedPhotosAlbum(path, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
}

//保存图片回调
+ (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSString *msg = nil ;
    if(error){
        msg = @"保存图片失败" ;
    }else{
        msg = @"保存图片成功" ;
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self  cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert show];
}

//保存视频回调
+ (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo: (void *)contextInfo {
    
    NSString *msg = nil ;
    if (error == nil) {
        msg = @"保存视频成功" ;
    }else{
        msg = @"保存视频失败" ;
    }
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:msg delegate:self  cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert show];
}
@end

